﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Windows.Data;

namespace WPFControls.ValueConverters
{
    /// <summary>
    /// 用于截取 UcConsole 的 TransactionId 信息
    /// </summary>
    public class UcConsole_TransactionId_MultiValueConverter : IMultiValueConverter
    {
        /// <summary>
        /// Convert
        /// </summary>
        /// <param name="values"></param>
        /// <param name="targetType"></param>
        /// <param name="parameter"></param>
        /// <param name="culture"></param>
        /// <returns></returns>
        public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        {
            object value = values[0];
            object objLen = values[1];

            int len = 4; // 默认保留 4 位
            if (objLen != null && int.TryParse(objLen.ToString(), out int tempInt))
            {
                len = tempInt;
            }

            if (value == null)
            {
                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < len; i++)
                {
                    sb.Append(" ");
                }
                return sb.ToString();
            }

            string d = value.ToString();
            return d.Substring(d.Length - len);
        }

        /// <summary>
        /// ConvertBack
        /// </summary>
        /// <param name="value"></param>
        /// <param name="targetTypes"></param>
        /// <param name="parameter"></param>
        /// <param name="culture"></param>
        /// <returns></returns>
        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
        {
            return null;
        }
    }
}
